<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" type="text/css" href="../lib/ext/resources/css/ext-all-neptune.css"/>
    <link rel="stylesheet" type="text/css" href="../lib/ckeditor/sample.css" />
    <script type="text/javascript" src="../lib/ext/ext-all.js"></script>
    <script type="text/javascript" src="../lib/gf/common_grid.js"></script>
    <script type="text/javascript" src="../lib/gf/common_alert.js"></script>
    <script type="text/javascript" src="../lib/gf/common_file.js"></script>

</head>
<script type="text/javascript">
    Ext.Loader.setConfig({enabled: true});

    Ext.require([
        'Ext.grid.*',
        'Ext.data.*',
        'Ext.util.*',
        'Ext.toolbar.Paging',
        'Ext.ModelManager',
        'Ext.tip.QuickTipManager'
    ]);

    var dataForm;
    var data = {};
    Ext.onReady(function () {
        //保存数据
        function doAdd(){
            if(!dataForm.form.isValid()){
                Ext.Msg.alert('系统提示','请修正指出错误，检查输入是否正确!');
                return;
            }
            var params = dataForm.form.getValues();
            var _url = '/picture/add';
            if(params && params._id ){
                _url = '/picture/edit';
            }
            Ext.Ajax.request({
                url: _url,
                params : params,
                method : 'POST',
                success: function(response, opts) {
                    alert_msg_refresh_close_win('业务处理成功！');
                },
                failure: function(response, opts) {
                    alert_msg(Ext.JSON.decode(response.responseText).error);
                }
            });

        }
        //关闭窗口
        function doClose(){
            parent.closeWin();
        }
        var tbar = [
            {pressed: true,text: '保  存',handler: doAdd,icon : '../img/book.png'},
            {pressed: true,text: "关  闭",handler: doClose,icon : '../img/close.png'}
        ];


        dataForm = new Ext.FormPanel({
            layout : 'fit',
            items: [
                {
                    xtype: 'form',
                    defaultType: 'textfield',
                    layout: {type: 'table',columns:3},
                    items: [
                        {
                            name : '_id',
                            hidden : true
                        },{
                            name : "_max_file_size",
                            value : "10000000", //文件最大大小10M
                            hidden : true
                        },
                        {fieldLabel: '图片标题',name: 'title', colspan : 3,width: 526,allowBlank: false},
                        {fieldLabel: '图片编号',name: 'code', colspan : 3,width: 526,allowBlank: false},
                        {xtype: "filefield",fieldLabel: '选择图片',name: 'attach_file',width:600, colspan : 2},
                        {
                            xtype: "button",
                            text: "上传图片",
                            handler: function () {
                                var type_array = ["BMP","JPG","GIF","PNG","PCX","DCX","EMF","JIF","JPE","JFIF","EPS","TIF","JPEG","RLE","DIB","PCD","DXF","ICO","WMF","TIFF","TGA"];
                                var value = dataForm.form.findField('attach_file').getValue();
                                if(!value){
                                    Ext.Msg.alert('消息提示', '请选择需要上传的图片!');
                                }
                                var index = value.lastIndexOf(".");
                                if(index==-1){
                                    Ext.Msg.alert('消息提示', '请选择图片文件!');
                                }else {
                                    var suffix = value.substring(index+1).toUpperCase();
                                    var flag = false;
                                    Ext.each(type_array, function (type, index){
                                        if(suffix==type) {
                                            flag = true;
                                        }
                                    });
                                    if(flag) {
                                        uploadFile(dataForm);
                                    }else{
                                        Ext.Msg.alert('消息提示', '请选择图片文件!');
                                    }
                                }
                            },
                            style: 'margin-left: 10px;'
                        },

                        {fieldLabel: '图片id',name: 'image_file_id',readOnly : true,colspan : 3,width: 526},

                    ],
                    tbar : tbar
                }
            ]
        });
        var viewport = new Ext.Viewport({
            layout: 'fit',
            renderTo: "mypanel",
            items: [dataForm]
        });



        //获取url中的参数，如果传入了参数_id那么进行修改；否则，添加。
        var _urlparams = Ext.urlDecode(document.URL.split("?")[document.URL.split("?").length - 1]);
        if(_urlparams && _urlparams._id){
            Ext.Ajax.request({
                url: '/picture/detail',
                params : {'_id' : _urlparams._id},
                method : 'get',
                success: function(response, opts) {
                    data =  Ext.decode(response.responseText);
                    init_form();
                },
                failure: function(response, opts) {
                    alert('加载失败......');
                    init_form();
                }
            });
        }

        function init_form(){
            if(!data){
                return ;
            }
            for(var i in data){
                if(dataForm.form.findField(i)){
                    dataForm.form.findField(i).setValue(data[i]);
                }
            }
        }

    });
</script>
<body>
<div id="mypanel" style="height:1024px;width:100%"></div>
</body>
</html>